//
// Created by tangjian on 10/13/23.
//

#include "fcdbc.h"

int hello() {
    printf("hello cdbc!");
}

//剪切文件夹
int cutPath(char *filePath, char **folderNames, int maxFolders) {
    char *pathCopy = strdup(filePath);  // 复制路径字符串
    char *token = strtok(pathCopy, "/");  // 使用斜杠作为分隔符获取第一个文件夹名称

    int folderCount = 0;
    while (token != NULL && folderCount < maxFolders) {
        folderNames[folderCount] = strdup(token);  // 复制文件夹名称到字符串数组
        token = strtok(NULL, "/");  // 继续获取下一个文件夹名称
        folderCount++;
    }

    free(pathCopy);  // 释放内存
}

int test_cutPath() {
    char *buf[10] = {0};
    char *path = "123/456/789/asd";
    int a = cutPath(path, buf, 10);
    int i;
    for (i = 0; i < 10 && buf[i] != NULL; i++) {
        printf("Folder %d: %s\n", i + 1, buf[i]);
        free(buf[i]);  // 释放每个文件夹名称的内存
    }
}

int cdbc_conf(cdbc_t *cdbc) {
    char *db_host = "127.0.0.1";
    strcpy(cdbc->db_host, db_host);
    char *db_name = "netdisk";
    strcpy(cdbc->db_name, db_name);
    char *db_pw = "123456";
    strcpy(cdbc->db_pw, db_pw);
    char *db_user = "root";
    strcpy(cdbc->db_user, db_user);
}

int cdbc_init(cdbc_t *cdbc) {
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *ret = mysql_real_connect(mysql, cdbc->db_host,
                                    cdbc->db_user, cdbc->db_pw,
                                    cdbc->db_name, 0, NULL, 0);

    if (ret == NULL) {
        fprintf(stderr, "mysql_real-connect:%s\n", mysql_error(mysql));
//        LOG_ERR(ret, NULL, mysql_error(mysql));
        return -1;
    }
    cdbc->mysql = mysql;
}

int cdbd_free(cdbc_t *cdbc) {
    mysql_store_result(cdbc->mysql);
    mysql_close(cdbc->mysql);
}

int test_insert() {
    cdbc_t cdbc;
    int ret_conf = cdbc_conf(&cdbc);
    int ret = cdbc_init(&cdbc);

    char sql[] = "insert into file_info (filename, filepath, user, pre_id, type, md5, tomb)\n"
                 "values ('.', '/tangjian/', 'tangjian', '-1', '1', '', '0');";

    int qret = mysql_query(cdbc.mysql, sql);
    if (qret != 0) {
        fprintf(stderr, "mysql query :%s\n", mysql_error(cdbc.mysql));
    }
    return 0;
}

int getFileInfo_byDirUser(MYSQL *mysql, vdirent_t *vdirent,
                          char *filename, char *username, char *pre_id) {

    char *pre_sql = "select id, filename, filepath, user, pre_id, type, md5, tomb"
                    " from file_info "
                    "where user = '%s' and filename = '%s' and pre_id = '%s';";
    char sql[200] = {0};
    sprintf(sql, pre_sql, username, filename, pre_id);
    int qret = mysql_query(mysql, sql);
    if (qret != 0) {
        fprintf(stderr, "mysql query :%s\n", mysql_error(mysql));
    }
    MYSQL_RES *res = mysql_store_result(mysql);
    int ret = vdirent_fill(vdirent, res);
    return 1;
}

int getFileInfo_byId(MYSQL *mysql, vdirent_t *vdirent, char *id) {
    char *pre_sql = "select id, filename, filepath, user, pre_id, type, md5, tomb "
                    "from file_info "
                    "where id = '%s';";
    char sql[200] = {0};
    sprintf(sql, pre_sql, id);
    int sqret = mysql_query(mysql, sql);
    if (sqret != 0) {
        fprintf(stderr, "mysql query :%s\n", mysql_error(mysql));
    }
    MYSQL_RES *res = mysql_store_result(mysql);
    int ret = vdirent_fill(vdirent, res);
}

int getCount_byMd5(MYSQL *mysql, char *md5) {
    int count = 0;
    char *pre_sql = "select count(*) "
                    "from file_info "
                    "where md5 = '%s';";
    char sql[200] = {0};
    sprintf(sql, pre_sql, md5);
    int qret = mysql_query(mysql, sql);
    if (qret != 0) {
        fprintf(stderr, "mysql query :%s\n", mysql_error(mysql));
    }
    MYSQL_RES *res = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(res);
    if (row != NULL && row[0] != NULL) {
        count = atoi(row[0]);
    }
    return count;
}

int insertFileInfo(MYSQL *mysql, vdirent_t *vdirent) {
    char *pre_sql = "insert into file_info "
                    "(filename, filepath, user, pre_id, type, md5, tomb)"
                    "value "
                    "('%s', '%s', '%s', '%s', '0', '%s', '0');";
    char sql[200] = {0};
    sprintf(sql, pre_sql, vdirent->d_fname, vdirent->d_fpath, vdirent->usr,
            vdirent->d_perid, vdirent->d_md5);
    int qret = mysql_query(mysql, sql);
    if (qret != 0) {
        fprintf(stderr, "mysql query :%s\n", mysql_error(mysql));
    }

    return 1;
}

int vdirent_fill(vdirent_t *vdirent, MYSQL_RES *mysqlRes) {
    MYSQL_ROW row;
    if ((row = mysql_fetch_row(mysqlRes)) != NULL) {
        if (row[0] != NULL) {
            memcpy(vdirent->d_id, row[0], strlen(row[0]));
        }
        if (row[1] != NULL) {
            memcpy(vdirent->d_fname, row[1], strlen(row[1]));
        }
        if (row[2] != NULL) {
            memcpy(vdirent->d_fpath, row[2], strlen(row[2]));
        }
        if (row[3] != NULL) {
            memcpy(vdirent->usr, row[3], strlen(row[3]));
        }
        if (row[4] != NULL) {
            memcpy(vdirent->d_perid, row[4], strlen(row[4]));
        }
        if (row[5] != NULL) {
            memcpy(vdirent->d_type, row[5], strlen(row[5]));
        }
        if (row[6] != NULL) {
            memcpy(vdirent->d_md5, row[6], strlen(row[6]));
        }
        if (row[7] != NULL) {
            memcpy(vdirent->d_tomb, row[7], strlen(row[7]));
        }
    }
}